package cn.javablog.controller;


import cn.javablog.Vo.PageVo;
import cn.javablog.Vo.TagVo;
import cn.javablog.common.lang.Result;
import cn.javablog.entity.TTag;
import cn.javablog.service.TTagService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.apache.shiro.util.Assert;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author AllenYe
 * @since 2020-09-12
 */
@RestController
@RequestMapping("/tag")
public class TTagController {
    @Autowired
    private TTagService tTagService;

    @PostMapping("/lists")
    public Result tagLists(@RequestBody PageVo pageVo){
        Page page = new Page(pageVo.getPageCurrent() == 0 ? 1 : pageVo.getPageCurrent(),pageVo.getPageSize() == 0 ? 10 : pageVo.getPageSize());
        QueryWrapper<TTag> wrapper = new QueryWrapper();
        IPage pageList = tTagService.page(page, wrapper);
        return Result.succ(pageList);
    }
    /**
     * 查询全部标签
     * @param pageVo
     * @return
     */
    @RequiresAuthentication
    @PostMapping("/list")
    public Result tagList(@RequestBody PageVo pageVo){
        Page page = new Page(pageVo.getPageCurrent() == 0 ? 1 : pageVo.getPageCurrent(),pageVo.getPageSize() == 0 ? 10 : pageVo.getPageSize());
        QueryWrapper<TTag> wrapper = new QueryWrapper();
        wrapper.like("name", pageVo.getName());
        IPage pageList = tTagService.page(page, wrapper);
        return Result.succ(pageList);
    }

    /**
     * 添加标签
     * @param tagVo
     * @return
     */
    @RequiresAuthentication
    @PostMapping("/add")
    public Result tagAdd(@Validated @RequestBody TagVo tagVo){
        TTag tag = tTagService.getOne(new QueryWrapper<TTag>().eq("name", tagVo.getName()));
        if(tag != null){
          return  Result.fail("标签名不能重复");
        }else{
            TTag tTag1 = new TTag().setName(tagVo.getName());
            boolean flag = tTagService.save(tTag1);
            if(flag){
                return   Result.succ("添加成功",null);
            }else{
                return  Result.fail("添加失败");
            }
        }
    }

    /**
     * 修改标签
     * @param tagVo
     * @return
     */
    @RequiresAuthentication
    @PostMapping("/edit")
    public Result tagEdit(@Validated @RequestBody TagVo tagVo){
        TTag tag = tTagService.getById(tagVo.getId());
        Assert.notNull(tag,"该标签不存在");
        TTag tagName = tTagService.getOne(new QueryWrapper<TTag>().eq("name", tagVo.getName().toLowerCase()));
        if(tagName!=null){
            if((tag.getId()!=tagName.getId())){
                return  Result.fail("标签名称不能重复");
            }
        }
        TTag tagData = new TTag();
        tagData.setId(tagVo.getId());
        tagData.setName(tagVo.getName());
        boolean editFlag = tTagService.update(tagData, new UpdateWrapper<TTag>().eq("id", tagVo.getId()));
        if(editFlag){
            return Result.succ("更新成功",null);
        }else{
            return Result.fail("更新失败",null);
        }
    }

    /**
     * 删除标签
     * @param id
     * @return
     */
    @RequiresAuthentication
    @GetMapping("/del")
    public Result tagDel(@RequestParam int id){
        TTag tag = tTagService.getOne(new QueryWrapper<TTag>().eq("id", id));
        Assert.notNull(tag,"该标签不存在");
        boolean delFlag = tTagService.removeById(id);
        if(delFlag){
            return Result.succ("删除成功",null);
        }else {
            return Result.fail("删除失败",null);
        }
    }
}
